package lcmpt;

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.util.Collection;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ListModel;
import javax.swing.SwingUtilities;
import javax.swing.event.HyperlinkListener;
import javax.swing.event.ListDataListener;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

import lcmpt.Controller.ComboHyperListener;
import de.dnb.basics.Misc;

public class View {

	private static final int MULTIPLIER = 8;
	private static final int DIVIDER = 10;
	GUI gui;

	public View() {
		super();
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					gui = new GUI();
					Dimension dimension =
							Toolkit.getDefaultToolkit().getScreenSize();
					gui.setBounds(dimension.width / DIVIDER, dimension.height
							/ DIVIDER, MULTIPLIER * dimension.width / DIVIDER,
							MULTIPLIER * dimension.height / DIVIDER);
					gui.setVisible(true);
					gui.splitPaneTreeInfo.setDividerLocation(.4);

				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * 
	 * @param node
	 *            wenn null, wird nichts geändert
	 */
	public final void setTree(final TreeNode node) {
		if (node == null)
			return;
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				gui.tree.setModel((new DefaultTreeModel(node)));
			}
		});
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				gui.editorPaneInfo.setCaretPosition(0);
				gui.scrollPaneTree.getVerticalScrollBar().getModel()
						.setValue(0);
				gui.scrollPaneTree.getHorizontalScrollBar().getModel()
						.setValue(0);
			}
		});
	}

	public void addTreeSelectionListener(final TreeSelectionListener listener) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				gui.tree.addTreeSelectionListener(listener);
			}
		});

	}

	public void addComboBoxListener(final ActionListener listener) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				gui.comboBoxNames.addActionListener(listener);
			}
		});
	}

	public void setInfo(final String info) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				gui.editorPaneInfo.setText(info);
			}
		});
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				gui.editorPaneInfo.setCaretPosition(0);

			}
		});
	}

	public String getSelectedName() {
		return (String) gui.comboBoxNames.getSelectedItem();
	}

	public void setNames(final Collection<String> names) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				gui.comboBoxNames.removeAllItems();
				for (String name : names) {
					gui.comboBoxNames.addItem(name);
				}
			}
		});

	}

	public void collapseTree() {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				Misc.expandAll(gui.tree, false);
			}
		});

	}

	public void expandTree(final TreePath path) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				gui.tree.setSelectionPath(path);
			}
		});
	}

	public void setPaths(final String s) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				gui.editorPanePaths.setText(s);
			}
		});
	}

	public void setTreeRenderer(final DefaultTreeCellRenderer renderer) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				gui.tree.setCellRenderer(renderer);
			}
		});
	}

	public void setLinkListener(final HyperlinkListener listener) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				gui.editorPaneInfo.addHyperlinkListener(listener);
			}
		});

	}

	public void addLcmptAusArchivListener(final ActionListener listener) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				gui.mntmLcmptAusArchiv.addActionListener(listener);
			}
		});

	}

	public void addLcmptAusNetzListener(final ActionListener listener) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				gui.mntmLcmptAusNetz.addActionListener(listener);
			}
		});

	}

	public void addLcgftAusArchivListener(final ActionListener listener) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				gui.mntmLcgftAusArchiv.addActionListener(listener);
			}
		});

	}

}
